﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="recObjectionForm.aspx.cs"
    Inherits="SIPO.Web.Forms.Transaction.recObjectionForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <link href="<%= ResolveUrl("~/styles/chosen/chosen.min.css") %>" rel="stylesheet"
        type="text/css" />
    <script src="<%= ResolveUrl("~/scripts/plupload/moxie.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/plupload/plupload.full.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.wrapper.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.inputmask.bundle.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/chosen/chosen.jquery.min.js") %>" type="text/javascript"></script>
    <script type="text/javascript">

        $("#btnSave").button({ icons: { primary: "ui-icon-disk"} });
        $("#btnClose").button({ icons: { primary: "ui-icon-close"} });

        $('#btnClose').click(function () {
            parent.$('#dialog').dialog("close");

            return false;
        });

    </script>
    <script type="text/javascript">

        $(document).ready(function () {

            $("#ddlSanksi").chosen({ placeholder_text_single: "-- pilih surat sanksi --" });
            $("#ddlDebitur").chosen({ placeholder_text_single: "-- pilih debitur --" });

        });

        function getSanction(debtorSectorId) {

            var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getsanction.ashx?method=objection") %>';

            $("#txtTglSanksi").val('');
            $("#txtNilaiSanksi").val('');
            var tglPermohonan = $("#txtTglSurat").val();

            $('#ddlSanksi').get(0).options.length = 0;
            $('#ddlSanksi').get(0).options[0] = new Option("-- pilih sanksi --", "");
            $('#ddlSanksi').trigger('chosen:updated');

            if (debtorSectorId > 0) {

                $.ajax({
                    type: 'POST',
                    url: handlerUrl + '&debtor_sector_id=' + debtorSectorId + '&tgl_permohonan=' + tglPermohonan,
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function (datas) {

                        if (datas.length > 1) {

                            $.each(datas, function (i, data) {

                                $('#ddlSanksi').append('<option value="' + data.SanctionID + '">' + data.LetterNo + '</option>');

                            });

                            $('#ddlSanksi').trigger('chosen:updated');

                        }
                        else if (datas.length == 1) {
                            $('#ddlSanksi').append('<option value="' + datas[0].SanctionID + '">' + datas[0].LetterNo + '</option>');
                            $('#ddlSanksi').val(datas[0].SanctionID);
                            $('#ddlSanksi').trigger('chosen:updated');

                            getSanctionById(datas[0].SanctionID);
                        }
                        else {
                            alert('Sanksi tidak ada, Periksa DebtorSectorID!');
                        }
                    },
                    error: function () {
                        alert('Error load data sanction!');
                    }
                });
            }

        }

        function getSanctionById(sanctionId) {

            var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getsanction.ashx?method=getsanctionbyid") %>';

            $("#txtTglSanksi").val('');
            $("#txtNilaiSanksi").val('');
            $("#hfSanctionNo").val('');
            $("#hfBillingNo").val('');

            if (sanctionId > 0) {

                $.ajax({
                    type: 'POST',
                    url: handlerUrl + "&sanction_id=" + sanctionId,
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function (data) {

                        $("#txtTglSanksi").val(data.LetterDate);
                        $("#txtNilaiSanksi").val(data.Value);
                        $("#hfSanctionNo").val(data.LetterNo);
                        $("#hfBillingNo").val(data.BillingNo);

                    },
                    error: function () {
                        alert('Error sanction !');
                    }
                });
            }

        }


    </script>
    <script type="text/javascript">

        $(document).ready(function () {

            $("#txtNoSurat").upperCase();

            var uploader = new plupload.Uploader({
                runtimes: 'html5,flash,silverlight',
                browse_button: 'pickfiles',
                container: 'container',
                max_file_size: '5mb',
                url: '<%= ResolveUrl("~/AjaxHandler/Transaction/recSanctionHandler.ashx") %>' + '?method=insert',
                flash_swf_url: '<%= ResolveUrl("~/scripts/plupload/Moxie.swf") %>',
                silverlight_xap_url: '<%= ResolveUrl("~/scripts/plupload/Moxie.xap") %>',
                filters: [
            { title: "Pdf files", extensions: "pdf" },
            { title: "Excel files", extensions: "xls,xlsx" },
			{ title: "Text files", extensions: "txt" },
			{ title: "Image files", extensions: "jpeg,jpg,gif,png" },
            { title: "Word files", extensions: "doc,docx" },
			{ title: "PowerPoint files", extensions: "ppt,pptx" }
		],
                resize: { width: 320, height: 240, quality: 90 }
            });

            uploader.bind('Init', function (up, params) {

                var msg = 'Untuk dapat melakukan proses unggah / upload dokumen, browser anda harus mendukung salah satu teknologi, HTML 5 / Flash / Silverlight';

                if (params.runtime == 'html5') {
                    msg = '';
                } else if (params.runtime == 'flash') {
                    msg = '';
                } else if (params.runtime == 'silverlight') {
                    msg = ''
                }

                if (msg != '') {
                    alert(msg);
                }

            });

            uploader.init();

            uploader.bind('FilesAdded', function (up, files) {

                if (up.files.length > 1) {
                    alert('Hanya diperbolehkan satu file untuk di upload!');
                    return false;
                }

                $.each(files, function (i, file) {
                    $('#filelist').append(
                        '<div id="' + file.id + '">' +
                file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' + '</div>');
                });

                up.refresh(); // Reposition Flash/Silverlight
            });

            uploader.bind('FileUploaded', function (up, file, info) {

                var err = JSON.parse(info.response);
                alert(err.Message);

            });

            uploader.bind('Error', function (up, err) {

                var errMsg = '';

                switch (err.code) {
                    case (-600):
                        errMsg = 'File tidak boleh lebih dari 5 Mb';
                        break;
                    case (-601):
                        errMsg = 'File harus berformat excel / word/ text / pdf / gambar';
                        break;
                    default:
                        errMsg = err.code + ' - ' + err.message;
                        break;
                }

                alert(errMsg);

                up.refresh(); // Reposition Flash/Silverlight

            });

            $("#txtNoSurat").inputmask("Regex", { regex: "[0-9/ .-:A-Za-z()-]*" });

            $("#txtNilaiSanksi").inputmask("decimal", { radixPoint: ",", autoGroup: true, groupSeparator: ".", groupSize: 3, digits: 2, repeat: 18 });

            $("#txtCatatan").inputmask("Regex", { regex: "[A-Za-z-,.0-9 ]*" });

            //            $('#txtNoSurat').focusout(function () {
            //                var objectionNo = $('#txtNoSurat').val();
            //                alert($('#txtNoSurat').val());
            //                var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/Loaddata/validasinoobjections.ashx?ObjectionNo=") %>';
            //                jQuery.ajax({
            //                    type: 'POST',
            //                    url: handlerUrl + objectionNo,
            //                    contentType: 'application/json',
            //                    dataType: 'json',
            //                    success: function (data) {
            //                        alert('a');
            //                        if (data.jumlah > 0) {
            //                            alert("Nomor surat. sudah ada !");
            //                            $('#txtNoSurat').val('');
            //                            $('#txtNoSurat').focus();
            //                            $("#txtNoSurat").css({ 'background-color': 'red' });
            //                        } else {
            //                            $("#txtNoSurat").css({ 'background-color': 'white' });
            //                        }

            //                    },
            //                    error: function () {
            //                        alert('Error validasi no. S.K. !');
            //                    }
            //                });
            //            });

            $("#txtTglSurat").inputmask("d/m/y", { "placeholder": "dd/mm/yyyy" });

            $("#txtTglSanksi").inputmask("d/m/y", { "placeholder": "dd/mm/yyyy" });

            

            var rules = {
                txtNoSurat: "required",
                txtTglSurat: "required",
                ddlSanksi: "required"
            };
            var messages = {
                txtNoSurat: "No Surat Keberatan harus di isi",
                txtTglSurat: "Tanggal Surat Keberatan harus di isi",
                ddlSanksi: "Sanksi harus di pilih"
            };

            var id = $('#<%= hfID.ClientID %>').val();

            // 2. Initiate the validator
            var validator = new jQueryValidatorWrapper("form1", rules, messages);

            $("#btnSave").click(function (e) {

                e.preventDefault();

                if (!validator.validate())
                    return;

                var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Transaction/recObjectionHandler.ashx") %>';

                if (id == '') {

                    if (uploader.files.length > 0) // Insert with uploaded file.
                    {
                        uploader.settings.multipart_params = {
                            'txtNoSurat': encodeURIComponent($('#txtNoSurat').val()),
                            'txtTglSurat': encodeURIComponent($('#txtTglSurat').val()),
                            'ddlSanksi': encodeURIComponent($('#ddlSanksi').val()),
                            'txtTglSanksi': encodeURIComponent($('#txtTglSanksi').val()),
                            'txtNilaiSanksi': encodeURIComponent($('#txtNilaiSanksi').val()),
                            'ddlDebitur': encodeURIComponent($('#ddlDebitur').val()),
                            'txtCatatan': encodeURIComponent($('#txtCatatan').val()),
                            'hfTransNo': encodeURIComponent($('#hfTransNo').val()),
                            'hfBillingNo': encodeURIComponent($('#hfBillingNo').val()),
                            'hfSanctionNo': encodeURIComponent($('#hfSanctionNo').val())
                        };

                        uploader.settings.url = handlerUrl + '?method=insert';
                        uploader.start();
                    }
                    else {

                        $.post('<%= ResolveUrl("~/AjaxHandler/Transaction/recObjectionHandler.ashx") %>' + '?method=insert',
                                $("#form1").serializeNoViewState())
                            .done(function (data) {
                                var r = JSON.parse(data);
                                alert(r.Message);
                            })
                            .fail(function () {
                                alert('Data gagal di simpan!');
                            });

                    }

                }
                else {

                    if (uploader.files.length > 0) // Update with uploaded file.
                    {
                        uploader.settings.multipart_params = {
                            'txtNoSurat': encodeURIComponent($('#txtNoSurat').val()),
                            'txtTglSurat': encodeURIComponent($('#txtTglSurat').val()),
                            'ddlSanksi': encodeURIComponent($('#ddlSanksi').val()),
                            'txtTglSanksi': encodeURIComponent($('#txtTglSanksi').val()),
                            'txtNilaiSanksi': encodeURIComponent($('#txtNilaiSanksi').val()),
                            'ddlDebitur': encodeURIComponent($('#ddlDebitur').val()),
                            'txtCatatan': encodeURIComponent($('#txtCatatan').val()),
                            'hfID': encodeURIComponent($('#hfID').val()),
                            'hfBillingNo': encodeURIComponent($('#hfBillingNo').val()),
                            'hfSanctionNo': encodeURIComponent($('#hfSanctionNo').val()),
                            'hfTransNo': encodeURIComponent($('#hfTransNo').val()),
                            'hfTransStatus': encodeURIComponent($('#hfTransStatus').val())
                        };

                        uploader.settings.url = handlerUrl + '?method=update';
                        uploader.start();

                    }
                    else {

                        $.post('<%= ResolveUrl("~/AjaxHandler/Transaction/recObjectionHandler.ashx") %>' + '?method=update',
                                $("#form1").serializeNoViewState())
                            .done(function (data) {
                                var r = JSON.parse(data);
                                alert(r.Message);
                            })
                            .fail(function (data) {
                                var r = JSON.parse(data);
                                alert(r.Message);
                            });

                    }

                }

                parent.$('#dialog').dialog('close');

                setTimeout("parent.$('#table').trigger('reloadGrid');", 3000);

                return false;

            });

            $('#ddlSanksi').change(function () {
                getSanctionById($(this).val());
            });

            $('#ddlDebitur').change(function () {
                var tglPermohonan = $("#txtTglSurat").val();
                if (tglPermohonan != "")
                    getSanction($(this).val());
                else {                    
                    alert('isi tanggal permohonan terlebih dahulu !');
                }
            });

            $("#txtTglSurat").datepicker({ showWeek: true, showButtonPanel: true,
                changeMonth: true, changeYear: true, dateFormat: 'dd/mm/yy'//, yearRange: '2010:2030'
            });


        })

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <table border="0">
        <tr>
            <td style="width: 50">
                No. Permohonan
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtNoSurat" runat="server" Width="100" MaxLength="20"></asp:TextBox>&nbsp;<span
                    style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Tgl. Permohonan
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtTglSurat" runat="server" Width="100"></asp:TextBox>&nbsp;<span
                    style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Nama Debitur
            </td>
            <td>
                :
            </td>
            <td>
                <asp:DropDownList ID="ddlDebitur" runat="server" Width="205">
                </asp:DropDownList>
                &nbsp;<span style="color: Red;">*</span>&nbsp;&nbsp;&nbsp;
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                No. Sanksi
            </td>
            <td>
                :
            </td>
            <td>
                <asp:DropDownList ID="ddlSanksi" runat="server" Width="205">
                </asp:DropDownList>
                &nbsp;<span style="color: Red;">*</span>&nbsp;&nbsp;&nbsp;
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Tgl. Sanksi
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtTglSanksi" runat="server" Width="100" Enabled="false"></asp:TextBox>&nbsp;
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Jumlah Sanksi
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtNilaiSanksi" Style="text-align: right" runat="server" Width="200"
                    MaxLength="100" Enabled="false"></asp:TextBox>&nbsp;
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Catatan
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtCatatan" TextMode="MultiLine" runat="server" MaxLength="10" Width="300"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Lampiran
            </td>
            <td>
                :
            </td>
            <td>
                <div id="container">
                    <a id="pickfiles" class="plupload_button plupload_add ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary"
                        href="#"><span class="ui-button-icon-primary ui-icon ui-icon-circle-plus" /><span
                            class="ui-button-text">Add Files</span> </a>
                    <br />
                    <div id="filelist">
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
            <td>
                <asp:HiddenField ID="hfID" runat="server" />
                <asp:HiddenField ID="hfSanctionNo" runat="server" />
                <asp:HiddenField ID="hfBillingNo" runat="server" />
                <asp:HiddenField ID="hfTransNo" runat="server" />
                <asp:HiddenField ID="hfTransStatus" runat="server" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
            <td>
                <div style="text-align: right; padding-top: 10px;">
                    <button id="btnSave" type="submit">
                        Simpan</button>
                    <button id="btnClose" type="button">
                        Batal</button>
                </div>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
